package org.midy.infrastructure;

import org.midy.infrastructure.tools.Logger;

import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * 业务路由分发总线接口的默认实现
 * InformationDistribution: 消息路由接口
 */
public class ClientInformationDistribution implements InformationDistribution {
    public static final Charset UTF8 = StandardCharsets.UTF_8;
    public static final Charset GBK = Charset.forName("GBK");

    @Override
    public void routeData(byte[] message, SocketChannel socketChannel, SelectionKey selectionKey, IOEventDrivenWorker worker) {
        String msg = new String(message, UTF8);
        Logger.message(msg);
    }

    @Override
    public void writeData(byte[] writeMessage, SocketChannel socketChannel, SelectionKey selectionKey, IOEventDrivenWorker worker) {
        worker.register(socketChannel, selectionKey, SelectionKey.OP_WRITE, writeMessage);
    }
}
